package cqut.wys.Controller;

import cqut.wys.websocket.SocketHandler;
import cqut.wys.websocket.TextSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.socket.TextMessage;

import javax.servlet.http.HttpSession;

/**
 * @author wuyoushan@zbj.com
 * @date 2017/3/21.
 */
@Controller
public class SocketController {

    @Autowired
    private SocketHandler socketHandler;

    @Autowired
    private TextSocketHandler textSocketHandler;

    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String login(HttpSession session){
        session.setAttribute("user","hejun");
        return "home";
    }

    @RequestMapping(value = "/message",method = RequestMethod.GET)
    public String sendMessage(){
        double rand=Math.ceil(Math.random()*100);
        socketHandler.sendMessageToUser("hejun",new TextMessage("Websocket测试消息" + rand));
        return "message";
    }

    //TextSocketHandler处理消息的，消息发送方法
    @RequestMapping(value = "/textmessage",method = RequestMethod.GET)
    public String sendTextMessage(){
        double rand=Math.ceil(Math.random()*100);
        textSocketHandler.sendTextMessage(new TextMessage("TextMessage 发送Websocket测试消息"+rand));
        return "message";
    }
}
